const axios = require('./api')

/**
 * 新增一条订单信息
 */
exports.add = (userId,items) => {
    return axios.post('orders',{
        user_id:userId,
        items
    }).then(res=>res.data).catch(err=>Promise.reject(err))
}

/**
 * 更改订单
 */
exports.update = orderInfo => {
    return axios.patch(`orders/${orderInfo.order_number}`,{
        pay_status:orderInfo.pay_status, // 支付状态
        send_status:orderInfo.send_status, // 发货状态
        trade_no:orderInfo.trade_no, // 支付宝流水号
        express_address:orderInfo.express_address // 收货地址
    }).then(res=>res.data).catch(err=>Promise.reject(err))
}

/**
 * 删除订单
 */
exports.delete = order_number => {
    return axios.delete(`orders/${order_number}`).then(res=>res.data).catch(err=>Promise.reject(err))
}

/**
 * 根据订单号，查询订单信息
 */
exports.single = (order_number) => {
    return axios.get(`orders/${order_number}`).then(res=>res.data).catch(err=>Promise.reject(err))
}

/**
 * 查询所有订单信息
 */
exports.all = (userId) => {
    return axios.get(`orders?user_id=${userId}`).then(res=>res.data).catch(err=>Promise.reject(err))
}